{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Compile PaddlePaddle Models\n**Author**: [Ziyuan Ma](https://github.com/ZiyuanMa/)\n\nThis article is an introductory tutorial to deploy PaddlePaddle models with Relay.\nFor us to begin with, PaddlePaddle>=2.1.3 is required to be installed.\nA quick solution is\n\n```bash\npip install paddlepaddle -i https://mirror.baidu.com/pypi/simple\n```\nor please refer to official site.\nhttps://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import tarfile\nimport paddle\nimport numpy as np\nimport tvm\nfrom tvm import relay\nfrom tvm.contrib.download import download_testdata"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Load pretrained ResNet50 model\nWe load a pretrained ResNet50 provided by PaddlePaddle.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "url = \"https://bj.bcebos.com/x2paddle/models/paddle_resnet50.tar\"\nmodel_path = download_testdata(url, \"paddle_resnet50.tar\", module=\"model\")\n\nwith tarfile.open(model_path) as tar:\n    names = tar.getnames()\n    for name in names:\n        tar.extract(name, \"./\")\n\nmodel = paddle.jit.load(\"./paddle_resnet50/model\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Load a test image\nA single cat dominates the examples!\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from PIL import Image\nimport paddle.vision.transforms as T\n\n\ntransforms = T.Compose(\n    [\n        T.Resize((256, 256)),\n        T.CenterCrop(224),\n        T.ToTensor(),\n        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n    ]\n)\n\nimg_url = \"https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true\"\nimg_path = download_testdata(img_url, \"cat.png\", module=\"data\")\nimg = Image.open(img_path).resize((224, 224))\n\nimg = transforms(img)\nimg = np.expand_dims(img, axis=0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Compile the model with relay\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "target = \"llvm\"\nshape_dict = {\"inputs\": img.shape}\nmod, params = relay.frontend.from_paddle(model, shape_dict)\n\nwith tvm.transform.PassContext(opt_level=3):\n    executor = relay.build_module.create_executor(\n        \"graph\", mod, tvm.cpu(0), target, params\n    ).evaluate()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Execute on TVM\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dtype = \"float32\"\ntvm_output = executor(tvm.nd.array(img.astype(dtype))).numpy()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Look up synset name\nLook up prediction top 1 index in 1000 class synset.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "synset_url = \"\".join(\n    [\n        \"https://gist.githubusercontent.com/zhreshold/\",\n        \"4d0b62f3d01426887599d4f7ede23ee5/raw/\",\n        \"596b27d23537e5a1b5751d2b0481ef172f58b539/\",\n        \"imagenet1000_clsid_to_human.txt\",\n    ]\n)\nsynset_name = \"imagenet1000_clsid_to_human.txt\"\nsynset_path = download_testdata(synset_url, synset_name, module=\"data\")\nwith open(synset_path) as f:\n    synset = f.readlines()\n\ntop1 = np.argmax(tvm_output[0])\nprint(f\"TVM prediction top-1 id: {top1}, class name: {synset[top1]}\")"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}